package com.borrow.jiemihua.mainUI.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.borrow.jiemihua.R;
import com.borrow.jiemihua.base.BaseActivity;
import com.borrow.jiemihua.base.BaseConstant;
import com.borrow.jiemihua.base.BaseRVAdapter;
import com.borrow.jiemihua.base.BaseViewHolder;
import com.borrow.jiemihua.base.URLConstant;
import com.borrow.jiemihua.bean.MyGoldBean;
import com.borrow.jiemihua.utils.SharedPreferencesUtils;
import com.borrow.jiemihua.utils.xutils3.MyCallBack;
import com.borrow.jiemihua.utils.xutils3.XUtil;
import com.borrow.jiemihua.view.pull.PullLoadMoreRecyclerView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyGoldActivity extends BaseActivity implements View.OnClickListener{
    private TextView tv_top_title;

    private PullLoadMoreRecyclerView recyclerView;
    private View head;
    private TextView tv_my_gold;
    private TextView tv_registe_num;
    private TextView tv_my_link;
    private RelativeLayout rl_iv_right;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_gold);
        head= View.inflate(MyGoldActivity.this,R.layout.activity_gold_head, null);
        initData();
        initView();
        upXQJFData();
        upJFPMData();
        initRecyclerData();

    }

    @Override
    protected void initData() {
        findViewById(R.id.imgbtn_left).setOnClickListener(this);
        rl_iv_right= (RelativeLayout) findViewById(R.id.rl_iv_right);
        rl_iv_right.setVisibility(View.VISIBLE);
        rl_iv_right.setOnClickListener(this);
        tv_top_title= (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("积分明细");
    }
    @Override
    protected void initView() {
        recyclerView= (PullLoadMoreRecyclerView) findViewById(R.id.recyclerView);
        tv_my_link= (TextView) head.findViewById(R.id.tv_my_link);
        tv_registe_num= (TextView) head.findViewById(R.id.tv_registe_num);
        tv_my_gold= (TextView) head.findViewById(R.id.tv_my_gold);
        recyclerView.setLinearLayout();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.imgbtn_left:
                finish();
                break;
            case R.id.rl_iv_right:
                jumpActivity(GoldGuiZeActivity.class,null,false);
                break;
            default:break;

        }
    }
    private BaseRVAdapter rvAdapter;
    private void initRecyclerData(){
        rvAdapter= new BaseRVAdapter(this,dataBeanList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.gold_recycleritem;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                holder.getTextView(R.id.tv_title).setText(dataBeanList.get(position).getSketch());
                holder.getTextView(R.id.tv_time).setText(dataBeanList.get(position).getCreateTime());
               String value= String.valueOf(dataBeanList.get(position).getValue());
                if(value.contains("-")){
                    holder.getTextView(R.id.tv_value).setText(value);
                }else{
                    holder.getTextView(R.id.tv_value).setText("+"+value);
                }

            }
        };
        recyclerView.setAdapter(rvAdapter);
        rvAdapter.addHeaderView(head);
        upGoldData(1);
        recyclerView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                upGoldData(1);
            }

            @Override
            public void onLoadMore() {
                upGoldData(2);
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        upGoldData(1);
    }

    private int page;
    private List<MyGoldBean.DataBean> dataBeanList =new ArrayList<>();
    private void upGoldData(final int i){
        Map<String, String> map = new HashMap<>();
        String userid = SharedPreferencesUtils.getString(this, BaseConstant.SPConstant.USERID, "");
        map.put("userId",userid);
        if (i == 1) {
            page = 1;
        } else {
            page++;
        }
        map.put("page", page+"");
        map.put("pageSize", 10+"");
        XUtil.Get(URLConstant.MY_GOLDMINGXI,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("==积分列表",result);
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    if(jsonObject.optString("isSuccess").equals("1")) {
                        Gson gson = new Gson();
                        MyGoldBean roomBean = gson.fromJson(result, MyGoldBean.class);
                        List<MyGoldBean.DataBean>  dataBeanList1 = roomBean.getData();
                        if (i == 1) {
                            dataBeanList.clear();
                        }

                        dataBeanList.addAll(dataBeanList1);
                        rvAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                recyclerView.setPullLoadMoreCompleted();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
            }
        });

    }
    private void upXQJFData(){
        Map<String, String> map = new HashMap<>();
        String userid = SharedPreferencesUtils.getString(this, BaseConstant.SPConstant.USERID, "");
        map.put("userId",userid);
        showDialog();
        XUtil.Get(URLConstant.CHAXUNJIFEN,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("==查询积分",result);
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    if(jsonObject.optString("isSuccess").equals("1")) {
                        String data=jsonObject.optString("data");
                        tv_my_gold.setText(data);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
                closeDialog();
            }
        });
    }
    private void upJFPMData(){
        Map<String, String> map = new HashMap<>();
        String userid = SharedPreferencesUtils.getString(this, BaseConstant.SPConstant.USERID, "");
        map.put("userId",userid);
        showDialog();
        XUtil.Get(URLConstant.JIFENPAIMING,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("==积分排名",result);
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    if(jsonObject.optString("isSuccess").equals("1")) {
                        String data=jsonObject.optString("data");
                        tv_my_link.setText(data);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
                closeDialog();
            }
        });

    }
}
